<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

jimport( 'joomla.application.component.model' );

/**
 * @package Joomla
 * @subpackage JobBoard
 */
class JobBoardModelJAConfiguration extends JModel
{
    var $_data;
    var $_table;
    /**
    * Return the query is used to retrive all row from database
    * @return string The query is used 
    */    
	
    /**
    * Get data list of configuration
    * @return array  Array of objects contain configuration data
    */
    function &getData(){
        // load data if it doesnnot already exists
        if(empty($this->_data)){
            $query = $this->_buildQuery(); 
            $this->_data = $this->_getList($query);
        }
        
        return $this->_data;
    }
    
    /**
    * Get configuration table instance
    * @return JTable Configuration table object
    */
    function &_getTable(){
        if($this->_table == null){
        	$this->_table = &JTable::getInstance('JAConfiguration', 'JobBoardTable');
		}
		return $this->_table;
	}		
		
	function getItems(){
		global $mainframe, $option;
		$tab = JRequest::getVar('tab','general');
		$db = &JFactory::getDBO();
		
		$query = "SELECT * "
        		 ."FROM #__ja_config WHERE tab='".$tab."'";
        		 
		$db->setQuery($query);
		
		$items = $db->loadObjectList();
		if(!$items){
			$items[0]->id = 0;
			$items[0]->data = '';
		}
		return $items[0];
	}
	/**
	* Get configuration item
	* @return Config Table object
	*/
	function getItem(){
		static $item;
		$tab = JRequest::getVar('tab');
		if (isset($item)) {
			return $item;
		}

		$table =& $this->_getTable();

		// Load the current item if it has been defined
		$cid = JRequest::getVar( 'cid', array(0), '', 'array' );
		JArrayHelper::toInteger($cid, array(0));
		if($cid)
			$table->load($cid[0]);
			$table->tab = $tab;
		
		$item = $table;
	    
	    return $item;	    
	}	
	/** 
	* Store configuration item
	* @param array The post array
	*/
	function store(){
	    // Initialize variables
		$db		=& JFactory::getDBO();
		$row	=& $this->getItem();
		$post	= $this->getState( 'request' );
		
		if (!$row->bind( $post )) {
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return false;
		}
		if (!$row->check())
		{
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return false;
		}
        
		if (!$row->store())
		{
			echo "<script> alert('".$row->getError(true)."'); window.history.go(-1); </script>\n";
			return false;
		}
		
		$row->checkin();
		return $row->id;
	}	
}
?>